package com.css.fxfzypg.modules.baseData.controller;

import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.baseData.entity.AnrrsmEntity;
import com.css.fxfzypg.modules.baseData.queryParams.AnrrsmQueryParams;
import com.css.fxfzypg.modules.baseData.service.AnrrsmService;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.zcpt.sys.service.SysAreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @create 2022-08-29 15:17
 *年度风险应对策略管理子模块
 */
@RestController
@RequestMapping("/fxfzypg/baseData")
@Slf4j
public class AnrrsmController {

    @Resource
    private AnrrsmService anrrsmService;


    @Resource
    private SysAreaService sysAreaService;
    @PostMapping("/saveAnrrsm")
    public RestResponse saveAnrrsm(@RequestBody AnrrsmEntity anrrsmEntity){



        try {
            String provinceId = anrrsmEntity.getProvinceId();
            String cityId = anrrsmEntity.getCityId();
            String prefectureId = anrrsmEntity.getPrefectureId();
            String address=provinceId+","+cityId+","+prefectureId;
            String areaNameByAreaId = sysAreaService.getAreaNameByAreaId(address);
            String[] split = areaNameByAreaId.split(",");
            anrrsmEntity.setArrsnProvince(split[0]);
            anrrsmEntity.setArrsmCity(split[1]);
            anrrsmEntity.setArrsmPrefecture(split[2]);
            AnrrsmEntity anrrsmEntity1 = anrrsmService.saveAnrrsm(anrrsmEntity);

            return RestResponse.succeed(anrrsmEntity1);
        } catch (Exception e) {
            log.error("新增年度风险应对策略管理数据失败",e);
            return RestResponse.fail("新增年度风险应对策略管理数据失败");
        }
    }
    @GetMapping("/getAnrrsmById/{id}")
    public RestResponse getAnrrsmById(@PathVariable("id")String id){
        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed(anrrsmService.getAnrrsmById(id));
        }catch (Exception e){
            log.error("获取失败!",e);
            restResponse = RestResponse.fail("获取失败!");
        }
        return restResponse;
    }


    @PostMapping("/updateAnrrsm")
    public RestResponse updateAnrrsm(@RequestBody AnrrsmEntity anrrsmEntity){
        try {
            String provinceId = anrrsmEntity.getProvinceId();
            String cityId = anrrsmEntity.getCityId();
            String prefectureId = anrrsmEntity.getPrefectureId();
            String address=provinceId+","+cityId+","+prefectureId;
            String areaNameByAreaId = sysAreaService.getAreaNameByAreaId(address);
            String[] split = areaNameByAreaId.split(",");
            anrrsmEntity.setArrsnProvince(split[0]);
            anrrsmEntity.setArrsmCity(split[1]);
            anrrsmEntity.setArrsmPrefecture(split[2]);



            AnrrsmEntity anrrsmEntity1= anrrsmService.updateAnrrsm(anrrsmEntity);
            return RestResponse.succeed(anrrsmEntity1);
        } catch (Exception e) {
            log.error("修改失败",e);
            return RestResponse.fail("修改失败");
        }
}

    @PostMapping("/deleteAnrrsm/{ids}")
    public RestResponse deleteAnrrsm(@PathVariable("ids") List<String> ids){
        try {
            anrrsmService.deleteAnrrsm(ids);

            return RestResponse.succeed("删除成功");
        } catch (Exception e) {
            log.error("删除失败",e);
            return RestResponse.fail("删除失败");
        }
    }

    @GetMapping("/getAllAnrrsm")
    public RestResponse getAllAnrrsm(HttpServletRequest request, AnrrsmQueryParams queryParams){
        RestResponse restResponse = null;
        try{


            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));

            queryParams.setCurPage(curPage);
            queryParams.setPageSize(pageSize);
            Map<String,Object> map = anrrsmService.getAllAnrrsmByParams(queryParams);
            restResponse = RestResponse.succeed(map);
        }catch (Exception e){
            log.error("查询失败!",e);
            restResponse = RestResponse.fail("查询失败");
        }
        return restResponse;
    }








}
